package com.tmzn.rewrite.entity;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 模仿php的array对象
 */
public class PhpMap {

    private Map<String,Object> map = new HashMap<>();

    public void put(String key,Object data){
        map.put(key,data);
    }

    public boolean isEmpty(){
        return map.isEmpty();
    }

    public Set<String> keySet(){
        return map.keySet();
    }

    public <T> T get(String key){
        Object o = map.get(key);
        if(o instanceof Number){
            if(o instanceof BigDecimal){
                return (T)getDouble(key);
            }
            if(o instanceof Double){
                return (T)getDouble(key);
            }
            if(o instanceof Float){
                return (T)getDouble(key);
            }
            return (T) getInt(key);
        }
        return (T)o;
    }

    public Double getDouble(String key){
        Object o = map.get(key);
        if(o == null){
            return null;
        }
        if(o instanceof Number){
            Number data = (Number)o;
            return data.doubleValue();
        }else{
            throw new RuntimeException("类型错误"+o.getClass());
        }
    }

    public Integer getInt(String key){
        Object o = map.get(key);
        if(o == null){
            return null;
        }
        if(o instanceof Number){
            Number cast = Number.class.cast(o);
            return cast.intValue();
        }else{
            throw new RuntimeException("类型错误"+o.getClass());
        }
    }
}
